package com.lanyan.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
        import com.lanyan.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.mapper.AccountMapper;
import com.lanyan.system.domain.Account;
import com.lanyan.system.domain.vo.AccountVo;
import com.lanyan.system.service.IAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 用户Service业务层处理
 *
 * @author lanyan
 * @date 2024-11-26
 */
@Service
public class AccountServiceImpl  extends ServiceImpl<AccountMapper,Account> implements IAccountService

{
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 查询用户
     *
     * @param accountId 用户主键
     * @return 用户
     */
    @Override
    public Account selectAccountByAccountId(Long accountId)
    {
        return this.getById(accountId);
    }

    /**
     * 查询用户Vo列表
     *
     * @param account 用户
     * @return 用户
     */
    @Override
    public List<AccountVo> selectAccountVoList(Account account)
    {
        return selectAccountList(account)
                .stream()
                .map(x -> (AccountVo)x)
                .collect(Collectors.toList());
    }
    /**
     * 查询用户列表
     *
     * @param account 用户
     * @return 用户
     */
    @Override
    public List<Account> selectAccountList(Account account)
    {
        LambdaQueryChainWrapper<Account> wrapper = this.lambdaQuery().eq(Account::getDelFlag,"0");
        if (StringUtils.isNotEmpty(account.getName())) {
            wrapper.like(Account::getName, account.getName());
        }
        if (StringUtils.isNotEmpty(account.getAvatar())) {
            wrapper.eq(Account::getAvatar, account.getAvatar());
        }
        if (StringUtils.isNotEmpty(account.getPhone())) {
            wrapper.eq(Account::getPhone, account.getPhone());
        }
        if (account.getOpenId() != null) {
            wrapper.eq(Account::getOpenId, account.getOpenId());
        }
        if (StringUtils.isNotEmpty(account.getIsFreeze())) {
            wrapper.eq(Account::getIsFreeze, account.getIsFreeze());
        }
        if (StringUtils.isNotEmpty(account.getToken())) {
            wrapper.eq(Account::getToken, account.getToken());
        }
        if (StringUtils.isNotEmpty(account.getIsVip())) {
            if (StringUtils.equals(account.getIsVip(),"0")){
                wrapper.isNull(AccountVo::getVipEnd);
            }else {
                wrapper.isNotNull(AccountVo::getVipEnd);
            }
        }
        if (account.getVipEnd() != null) {
            wrapper.eq(Account::getVipEnd, account.getVipEnd());
        }
        if (account.getCreateTimeStart()!= null && account.getCreateTimeEnd()!= null){
            wrapper.between(Account::getUpdateTime,account.getCreateTimeStart(),account.getCreateTimeEnd());
        }
        if (account.getIsActive()!= null){
            if (account.getIsActive()){
                wrapper.ge(Account::getUpdateTime, DateUtils.addDays(new Date(), -3));
            }
        }
        return wrapper.list();
    }

        /**
         * 新增用户
         *
         * @param account 用户
         * @return 结果
         */
    @Override
        public boolean insertAccount(Account account)
        {
                    account.setCreateTime(DateUtils.getNowDate());
                return this.save(account);
        }

        /**
         * 修改用户
         *
         * @param account 用户
         * @return 结果
         */
    @Override
        public boolean updateAccount(Account account)
        {
                    account.setUpdateTime(DateUtils.getNowDate());
            return this.updateById(account);
        }

        /**
         * 批量删除用户
         *
         * @param accountIds 需要删除的用户主键
         * @return 结果
         */
    @Override
        public boolean deleteAccountByAccountIds(Long[] accountIds)
        {
            return this.lambdaUpdate().set(Account::getDelFlag,"1").in(Account::getAccountId,accountIds).update();
        }

        /**
         * 删除用户信息
         *
         * @param accountId 用户主键
         * @return 结果
         */
    @Override
        public boolean deleteAccountByAccountId(Long accountId)
        {
            return this.lambdaUpdate().set(Account::getDelFlag,"1").eq(Account::getAccountId,accountId).update();
        }
    }
